package com.example.controller;

import com.example.pojo.Student;
import com.example.pojo.Users;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/* 交给spring容器产生对象 */
@Controller
@RequestMapping("/demo")
public class DemoController {

    /* 接口方法 */
    /* localhost:8080/ssm_project/demo/fun01 */
    @RequestMapping("/fun01")
    @ResponseBody // 这是响应个数据
    public Object fun01() {
        System.out.println("用户请求了fun01");
        return "fun01";
    }

    /* get：/ssm_project/demo/fun02?a=1&b=张三 */
    /* post：/ssm_project/demo/fun02 使用x-www-form-urlencoded携带参数 */
    /* post方式：接收中文参数乱码 */
    @RequestMapping("/fun02")
    @ResponseBody
    public Object fun02(int a, String b) {
        System.out.println(a);
        System.out.println(b);
        return "a:" + a + ",b:" + b;
    }

    /* method：限制请求方式为post */
    /* RequestParam：形参取别名，实际请求参数为注解的值 */
    @RequestMapping(method = RequestMethod.POST,
            value = "/fun03")
    @ResponseBody
    public Object fun03(@RequestParam("uage") int a,
                        @RequestParam("uname") String b) {
        Users users = new Users(null, b, a);
        System.out.println(users);
        return users;
    }

    /* 使用post方式分别发送多个参数，与对象的属性一致即可 */
    @RequestMapping(method = RequestMethod.POST,
            value = "/fun04")
    @ResponseBody
    public Object fun04(Users users) {
        System.out.println(users);
        return users;
    }

    /* 传递复杂对象：子对象使用grade.id、grade.number、grade.major传值 */
    @RequestMapping(method = RequestMethod.POST,
            value = "/fun05")
    @ResponseBody
    public Object fun05(Student student) {
        System.out.println(student);
        return student;
    }

    @RequestMapping(method = RequestMethod.POST,
            value = "/fun06")
    @ResponseBody // 响应的对象转json
    // @RequestBody 请求的JSON文本转java对象
    public Object fun06(@RequestBody Student student) {
        System.out.println(student);
        return student;
    }

    /* JSON转化成集合 */
    @RequestMapping(method = RequestMethod.POST,
            value = "/fun07")
    @ResponseBody
    public Object fun07(@RequestBody List<Student> students) {
        System.out.println(students);
        return students;
    }

    @RequestMapping("/fun08")
    @ResponseBody
    // @DateTimeFormat自动转化字符串为时间对象
    public Object fun08(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date1,
                        String date2) throws ParseException {
        System.out.println("date1:" + date1);
        // 纯文本的时间不能进行时间运算
        System.out.println("date2:" + date2);
        DateFormat df =
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = df.parse(date2);
        System.out.println(parse);
        return "success";
    }
}
